package com.ww.springboot.boot.javabase.proxy.javaInvocation2;

import org.springframework.transaction.annotation.Transactional;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Proxy;
import java.util.Map;

/**
 * 描述：
 *
 * @author 🧑 ‍wanwei
 * @since 2022-04-11 18:48
 */
public class ProxyDemo1 {
    public static void main(String[] args) throws NoSuchFieldException, IllegalAccessException {
        //通过方法获取注解
        Transactional annotation = TestDto.class.getAnnotation(Transactional.class);

        System.out.println(annotation.timeout());

        if (annotation != null) {
            InvocationHandler invocationHandler = Proxy.getInvocationHandler(annotation);
            Field values = invocationHandler.getClass().getDeclaredField("memberValues");
            values.setAccessible(true);
            Map<String, Object> memberValues = (Map<String, Object>) values.get(invocationHandler);
            int val = (int) memberValues.get("timeout");
            System.out.println("改变前:" + val);
            memberValues.put("timeout", 456);
            System.out.println("改变后:" + annotation.timeout());
        }
    }
}
